/********************************************************/
/*	Project:					*/
/*	Author:		gong_libin			*/
/*	Date:		2016_01_01			*/
/*	File:		GlbTest.cpp			*/
/********************************************************/

#include "GlbBson.h"

void GlbDisplay(CGlbBson& rCBson)
{
	CGlbBson CChild;

	while (true == rCBson.GlbBsonIterNext()) {
		GLB__PRINT("%s: ", rCBson.GlbBsonIterKey());
		switch (rCBson.GlbBsonIterType()) {
			case BSON_TYPE_DOUBLE:
				GLB__PRINT("%f, ", rCBson.GlbBsonGetDouble());
				break;
			case BSON_TYPE_UTF8:
				GLB__PRINT("%s, ", rCBson.GlbBsonGetUtf8(NULL));
				break;
			case BSON_TYPE_DOCUMENT:
				CChild.GlbBsonGetDoc(rCBson.GlbBsonGetIter());
				GlbDisplay(CChild);
				break;
			case BSON_TYPE_ARRAY:
				CChild.GlbBsonGetArray(rCBson.GlbBsonGetIter());
				GlbDisplay(CChild);
				break;
			case BSON_TYPE_BOOL:
				GLB__PRINT("%d, ", rCBson.GlbBsonGetBool());
				break;
			case BSON_TYPE_INT32:
				GLB__PRINT("%d, ", rCBson.GlbBsonGetInt32());
				break;
			case BSON_TYPE_INT64:
				GLB__PRINT("%lld, ", rCBson.GlbBsonGetInt64());
				break;
			default:
				break;
		}
	}

	return;
}

int main(int argc, char* argv[])
{
	/* Bson to Json */
	UINT uiJson = 0;
	CGlbBson CRoot;
	CGlbBson CArray;
	char* pszJson = NULL;

	CArray.GlbBsonInit();
	CArray.GlbBsonPutInt32("0", 32);
	CArray.GlbBsonPutInt64("1", 64);
	CArray.GlbBsonPutUtf8("2", (char*)"utf8");
	CArray.GlbBsonPutDouble("3", 123456.123456);

	CRoot.GlbBsonInit();
	CRoot.GlbBsonPutUtf8("ID", (char*)"001");
	CRoot.GlbBsonPutArray("ARR", CArray.GlbBsonGetBson());

	pszJson = CRoot.GlbBsonAsJson(&uiJson);
	GLB__PRINT("Json: %s\n", pszJson);
	CRoot.GlbBsonFree(pszJson);

	/* Json to Bson */
	CRoot.GlbBsonIterInit();
	GlbDisplay(CRoot);
	GLB__PRINT("\n");

	CArray.GlbBsonDestroy();
	CRoot.GlbBsonDestroy();

	return GLB__SUCCESS;
}
